#ifndef __BASE_SINGLETON_H_
#define __BASE_SINGLETON_H_

#include "Mutex.h"

namespace BASE{

	template<typename T>
	class Singleton{
		public:
			static void init(){
				Lock lock(mutex);
				val = new T();
			}

			static void destroy(){
				Lock lock(mutex);
				if (val != NULL){
					delete val;
					val = NULL;
				}
			}

			static T* get(){
				if (val != NULL){
					return val;
				}
				Lock lock(mutex);
				if (val != NULL){
					return val;
				}
				val = new T();
				return val;
			}
		public:
			static Mutex mutex;
			static T * val;
	};
	template<typename T> T * Singleton<T>::val = NULL;
	template<typename T> Mutex Singleton<T>::mutex;
}
#endif

